<% remote_form_for :milestone, milestone, :url => polymorphic_path([:admin, milestone]), :html => {:class => "remote_form", :id => "milestone_form", :method => milestone.new_record? ? :post : :put } do |milestone_form| %>
  <fieldset>
    <legend><%= milestone.new_record? ? "Milestone Info" : "Update #{milestone.title}" %></legend>
    <div class="form_group">
      <div class="form_pair">
        <%= milestone_form.label :title %>
        <%= milestone_form.text_field :title, :class => "span-16" %>
      </div>
    </div>

    <div class="form_group">   
      <div class="form_pair span-6">
        <label for="milestone_started_on_date_picker">Started On</label>
        <%= milestone_form.text_field :started_on, :id => "milestone_started_on_date_picker", :class => "date_picker", :value => milestone.started_on.blank? ? "" : milestone.started_on.to_s(:numeric) %>
      </div>

      <div class="radio_group span-4" id="milestone_status">
        <%= milestone_form.label :status_code, "Status" %>
        <%= milestone_form.select(:status_code, STATUS_CODES.collect{|p| [ p[:text], p[:db_code] ] }, { :include_blank => false }) %>
      </div>

      <div class="form_pair span-6 last" id="milestone_completed_on_box" style="display:<%= (milestone.status_code == 3) ? "block" : "none" %>;">
        <label for="milestone_completed_on_date_picker">Completed On</label>
        <%= milestone_form.text_field :completed_on, :id => "milestone_completed_on_date_picker", :class => "date_picker", :value => milestone.completed_on.blank? ? "" : milestone.completed_on.to_s(:numeric) %>
      </div>     
    </div>

    <div class="form_group">
      <div class="form_pair">
        <label for="milestone_description_markdown">Description <span class="quiet">(uses Markdown)</span></label>
        <%= milestone_form.text_area :description_markdown, :class => "markdown_support" %>
        <p class="clear" style="margin-bottom:0;"><%= link_to_function "Preview", "toggle_markdown_preview('milestone_description_markdown')" %> | <%= link_to "What the hell is Markdown?", "http://daringfireball.net/projects/markdown/syntax" %></p>
        <div class="markdown_help">
          <div id="preview_milestone_description_markdown" class="markdown_preview hidden"></div>
        </div>
      </div>
    </div>
    <div class="buttons">
      <%= milestone_form.hidden_field :project_id, :value => @project.id %>
      <%= milestone_form.submit "Save Milestone" %> or <%= link_to_remote("Cancel", :update => "milestone_#{milestone.id}", :url => admin_milestone_path(milestone), :method => :get) unless milestone.new_record? %>
    </div>
  </fieldset>
<% end %>

<% unless milestone.new_record? %>
  <fieldset>
    <legend>Update Tasks for <%= milestone.title %></legend>
    <table class="tasks clear">
      <thead>
        <tr>
          <th>Title</th>
          <th>Sub-Contractor</th>
          <th>Started</th>
          <th>Status</th>
          <th>Completed</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody id="milestone_tasks">
        <% milestone.tasks.each do |task| %>
          <%= render :partial => "admin/tasks/show", :locals => {:task => task, :show_actions => true, :row_wrap => true} %>
        <% end %>
      </tbody>
      <tbody>
        <tr class="table_bottom">
          <th colspan="5"></th>
          <th><%=link_to_remote "New", :update => "task_form_row" , :url => show_remote_form_admin_milestone_task_url(milestone, "new"), :method => :get, :html => { :class  => "task_action" } %></th>
        </tr>
      </tbody>
      <tbody id="task_form_row"></tbody>
    </table>
  </fieldset>
<% end %>
  

<script type="text/javascript">
  $j( "#milestone_started_on_date_picker" ).datepicker({ nextText: '&rarr;', prevText: '&larr;' });
  $j( "#milestone_completed_on_date_picker" ).datepicker({ nextText: '&rarr;', prevText: '&larr;' });
  
  $j("#milestone_status_code").bind("change", function() {
    if(this.value == "3"){
      $j("#milestone_completed_on_box").fadeIn();
    }else{
      $j("#milestone_completed_on").val("");
      $j("#milestone_completed_on_box").fadeOut();
    }
  });
  
  $j("#milestone_description_markdown").bind("keyup",function() {
    var preview_element   = $j("#preview_" + this.id);
    if ( !preview_element.hasClass("hidden") ){
      preview_markdown(this.id);
    }
  });
  
</script>